from django.db import models
from students.models import Student
from grades.models import Grade


class Score(models.Model):
	student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='scores', verbose_name='学生')
	grade = models.ForeignKey(Grade, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='班级')
	subject = models.CharField(max_length=100, verbose_name='科目')
	score = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='分数')
	term = models.CharField(max_length=50, verbose_name='学期')
	date_recorded = models.DateField(auto_now_add=True, verbose_name='记录日期')

	created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
	updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')

	class Meta:
		verbose_name = '成绩'
		verbose_name_plural = '成绩'
		ordering = ['-date_recorded']

	def __str__(self):
		return f"{self.student.name} - {self.subject}: {self.score}"
